<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
use \think\Request;
use \think\facade\Config;//引入配置
use PHPMailer\PHPMailer\PHPMailer;//引入邮件类
use TCPDF;
use think\captcha\Captcha;
use \think\facade\Session;
use OSS\OssClient;
use OSS\Core\OssException;

class Pdf extends Controller
{


    public function pdf($file,$name,$filename){
        set_time_limit(0);
        $custom_layout = array(297,210);
        $pdf = new TCPDF('Landscape', PDF_UNIT, $custom_layout, true, 'UTF-8', false);
        $Product_Number=Db::table('lily_products')->field('part_number,url_product')->where('url_product','=',explode('.', $filename)[0])->find();
        // set document information
        $pdf->SetCreator('-');
        $pdf->SetTitle($Product_Number['part_number'].' '.$name);
        $pdf->SetAuthor('LILY BEARING');
        $pdf->SetSubject('-');
        $pdf->SetKeywords('-');
        

        // remove default header/footer
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(0, 0, 0);
        //$pdf->SetFooterMargin(0);

        // set auto page breaks
        $pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // set some language-dependent strings (optional)
        if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
            require_once(dirname(__FILE__).'/lang/eng.php');
            $pdf->setLanguageArray($l);
        }

        // ---------------------------------------------------------

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        $bMargin = $pdf->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $pdf->getAutoPageBreak();
        // disable auto-page-break
        $pdf->SetAutoPageBreak(false, 0);
        // set bacground image
        $pdf->Image($file, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        $pdf->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $pdf->setPageMark();
        
        $load=explode('application', __DIR__);
        $dir=$load[0]."public/static/tcpdf/";
       
        $pdf->SetFillColor(255, 255, 255);
        $pdf->MultiCell(31, 26, ' ', 'TLR', 'J', 1, 1, '185', '175', true, 0, false, true, 26, 'M');
        $pdf->Image($dir.'pdf-logo.jpg', 185.5, 175.5, 30);
        $pdf->setCellPaddings('2','3','2','2');
        $pdf->MultiCell(76.2, 10, '<a style="color:black;text-align:center;font-size:12px;text-decoration:none" href="https://www.lily-bearing.com/">SHANGHAI LILY BEARING LIMITED</a></br>', 'T', 'J', 1, 1, '216.2', '175', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('2','3','2','2');
        $pdf->MultiCell(73.3, 10, '<a class="email" style="color:black;text-align:center;font-size:12px;text-decoration:none" href="mailto:lilybearing@lily-bearing.com">Email: lilybearing@lily-bearing.com</a>', 0, 'J', 1, 1, '219.2', '180', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('2','3.1','2','2');

        $pdf->MultiCell(20, 22, '<div style="text-align:center;font-size:14px;">Part Number</div>', 'TLR', 'J', 1, 1, '185', '190.95', true, 0, true, true, 22, 'M');
        
        $pdf->setCellPaddings('2','1.8','2','2');
        $pdf->MultiCell(87.3, 10, '<a class="email" style="color:black;text-align:center;font-size:14px;text-decoration:none" href="https://www.lily-bearing.com/products/'.$Product_Number['url_product'].'/">'.$Product_Number['part_number'].'</a>', 'T', 'J', 1, 0, '205.15', '190.95', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('1','1','1','1');
        $pdf->MultiCell(87.3, 10, '<a class="email" style="color:black;text-align:center;font-size:12px;text-decoration:none" href="https://www.lily-bearing.com/products/'.$Product_Number['url_product'].'/">'.$name.'</a>', 0, 'J', 1, 0, '205.15', '197.95', true, 0, true, true, 10, 'M');


        //ob_end_clean();
        /*I浏览器打开，D下载,F保存服务器，S字符串输出，E邮件输出*/
        $pdf->Output($dir.'pdf/'.explode('.', $filename)[0].".pdf", 'F');
    }

    public function pdf1($file,$name,$filename){
        set_time_limit(0);
        $custom_layout = array(297,210);
        $pdf = new TCPDF('Landscape', PDF_UNIT, $custom_layout, true, 'UTF-8', false);
        $Product_Number=Db::table('lily_products')->field('part_number,url_product')->where('url_product','=',explode('.', $filename)[0])->find();
        // set document information
        $pdf->SetCreator('-');
        $pdf->SetTitle($Product_Number['part_number'].' '.$name);
        $pdf->SetAuthor('LILY BEARING');
        $pdf->SetSubject('-');
        $pdf->SetKeywords('-');
        

        // remove default header/footer
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(0, 0, 0);
        //$pdf->SetFooterMargin(0);

        // set auto page breaks
        $pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // set some language-dependent strings (optional)
        if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
            require_once(dirname(__FILE__).'/lang/eng.php');
            $pdf->setLanguageArray($l);
        }

        // ---------------------------------------------------------

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        $bMargin = $pdf->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $pdf->getAutoPageBreak();
        // disable auto-page-break
        $pdf->SetAutoPageBreak(false, 0);
        // set bacground image
        $pdf->Image($file, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        $pdf->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $pdf->setPageMark();
        $load=explode('application', __DIR__);
        $dir=$load[0]."public/static/tcpdf/";
       
        $pdf->SetFillColor(255, 255, 255);
        $pdf->MultiCell(31, 26, ' ', 'TLR', 'J', 1, 1, '185', '166.4', true, 0, false, true, 26, 'M');
        $pdf->Image($dir.'pdf-logo.jpg', 185.5, 166.9, 30);
        $pdf->setCellPaddings('2','3','2','2');
        $pdf->MultiCell(76.2, 10, '<a style="color:black;text-align:center;font-size:12px;text-decoration:none" href="https://www.lily-bearing.com/">SHANGHAI LILY BEARING LIMITED</a></br>', 'T', 'J', 1, 1, '216.2', '166.4', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('2','3','2','2');
        $pdf->MultiCell(73.3, 10, '<a class="email" style="color:black;text-align:center;font-size:12px;text-decoration:none" href="mailto:lilybearing@lily-bearing.com">Email: lilybearing@lily-bearing.com</a>', 0, 'J', 1, 1, '219.2', '171.4', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('2','3.1','2','2');

        $pdf->MultiCell(20, 22, '<div style="text-align:center;font-size:14px;">Part Number</div>', 'TLR', 'J', 1, 1, '185', '182.35', true, 0, true, true, 22, 'M');
        
        $pdf->setCellPaddings('2','1.8','2','2');
        $pdf->MultiCell(87.3, 10, '<a class="email" style="color:black;text-align:center;font-size:14px;text-decoration:none" href="https://www.lily-bearing.com/products/'.$Product_Number['url_product'].'/">'.$Product_Number['part_number'].'</a>', 'T', 'J', 1, 0, '205.15', '182.35', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('1','1','1','1');
        $pdf->MultiCell(87.3, 10, '<a class="email" style="color:black;text-align:center;font-size:12px;text-decoration:none" href="https://www.lily-bearing.com/products/'.$Product_Number['url_product'].'/">'.$name.'</a>', 0, 'J', 1, 0, '205.15', '189.35', true, 0, true, true, 10, 'M');


        //ob_end_clean();
        /*I浏览器打开，D下载,F保存服务器，S字符串输出，E邮件输出*/
        $pdf->Output($dir.'pdf/'.explode('.', $filename)[0].".pdf", 'F');
    }
    
    /*测试用*/
    public function ceshi(){
        $file='/www/wwwroot/lily/public/static/tcpdf/file/3766n11.jpg';
        $name='111';
        $filename='3766n11.jpg';

        $custom_layout = array(297,210);
        $pdf = new TCPDF('Landscape', PDF_UNIT, $custom_layout, true, 'UTF-8', false);
        $Product_Number=Db::table('product_middle_table')->field('Product_Number,URL_Product')->where('URL_Product','=',explode('.', $filename)[0])->find();
        // set document information
        $pdf->SetCreator('-');
        $pdf->SetTitle($Product_Number['Product_Number'].' '.$name);
        $pdf->SetAuthor('LILY BEARING');
        $pdf->SetSubject('-');
        $pdf->SetKeywords('-');
        

        // remove default header/footer
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(0, 0, 0);
        //$pdf->SetFooterMargin(0);

        // set auto page breaks
        $pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // set some language-dependent strings (optional)
        if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
            require_once(dirname(__FILE__).'/lang/eng.php');
            $pdf->setLanguageArray($l);
        }

        // ---------------------------------------------------------

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        $bMargin = $pdf->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $pdf->getAutoPageBreak();
        // disable auto-page-break
        $pdf->SetAutoPageBreak(false, 0);
        // set bacground image
        $pdf->Image($file, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        $pdf->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $pdf->setPageMark();

       
        $pdf->SetFillColor(255, 255, 255);
        $pdf->MultiCell(31, 26, ' ', 'TLR', 'J', 1, 1, '185', '166.4', true, 0, false, true, 26, 'M');
        $pdf->Image('/www/wwwroot/lily/public/static/tcpdf/pdf-logo.jpg', 185.5, 166.9, 30);
        $pdf->setCellPaddings('2','3','2','2');
        $pdf->MultiCell(76.2, 10, '<a style="color:black;text-align:center;font-size:12px;text-decoration:none" href="https://www.lily-bearing.com/">SHANGHAI LILY BEARING LIMITED</a></br>', 'T', 'J', 1, 1, '216.2', '166.4', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('2','3','2','2');
        $pdf->MultiCell(73.3, 10, '<a class="email" style="color:black;text-align:center;font-size:12px;text-decoration:none" href="mailto:lilybearing@lily-bearing.com">Email: lilybearing@lily-bearing.com</a>', 0, 'J', 1, 1, '219.2', '171.4', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('2','3.1','2','2');

        $pdf->MultiCell(20, 22, '<div style="text-align:center;font-size:14px;">Part Number</div>', 'TLR', 'J', 1, 1, '185', '182.35', true, 0, true, true, 22, 'M');
        
        $pdf->setCellPaddings('2','1.8','2','2');
        $pdf->MultiCell(87.3, 10, '<a class="email" style="color:black;text-align:center;font-size:14px;text-decoration:none" href="https://www.lily-bearing.com/products/'.$Product_Number['URL_Product'].'/">'.$Product_Number['Product_Number'].'</a>', 'T', 'J', 1, 0, '205.15', '182.35', true, 0, true, true, 10, 'M');
        $pdf->setCellPaddings('1','1','1','1');
        $pdf->MultiCell(87.3, 10, '<a class="email" style="color:black;text-align:center;font-size:12px;text-decoration:none" href="https://www.lily-bearing.com/products/'.$Product_Number['URL_Product'].'/">'.$name.'</a>', 0, 'J', 1, 0, '205.15', '189.35', true, 0, true, true, 10, 'M');


        ob_end_clean();
        /*I浏览器打开，D下载,F保存服务器，S字符串输出，E邮件输出*/
        $pdf->Output('/www/wwwroot/lily/public/static/tcpdf/pdf/'.explode('.', $filename)[0].".pdf", 'I');
    }

    
    public function index(){
        return $this->fetch();
    }

    public function all_list(Request $request){
        set_time_limit(0);
        $post=$request->get();
        $load=explode('application', __DIR__);
        $dir=$load[0]."public/static/tcpdf/file/";
        if(is_dir($dir)){
            $info = opendir($dir);
            while (($file = readdir($info)) !== false) {
                if($file!='..'&&$file!='.'){
                    $chicun=$post['chicun'];

                    if($chicun==0){
                        $this->pdf($dir.$file,trim($post['name']),$file);
                    }
                    if($chicun==1){
                        $this->pdf1($dir.$file,trim($post['name']),$file);
                    }
                }   
           }
           closedir($info);
       }
    }



}
